﻿/***************************************************
//  Copyright (c) Premium Tax Free 2011
/***************************************************/

using System;
using System.ServiceProcess;
using System.Threading;
using PremierTaxFree.PTFLib.Data;
using PremierTaxFree.PTFLib;

namespace TransferringService
{
    public partial class XmlTransferringService : ServiceBase
    {
        private readonly TransferringWorker m_TransferringWorker = new TransferringWorker();

        public XmlTransferringService()
        {
            InitializeComponent();

            SQLWorker.Default.Error += OnError;
            m_TransferringWorker.Error += OnError;

            m_TransferringWorker.SleepTime = TimeSpan.FromHours(2).Milliseconds;
        }

        ~XmlTransferringService()
        {
            SQLWorker.Default.Error -= OnError;
            m_TransferringWorker.Error -= OnError;
        }

        protected override void OnStart(string[] args)
        {
            m_TransferringWorker.Start(ThreadPriority.Normal);
        }

        protected override void OnStop()
        {
            m_TransferringWorker.Stop();
        }

        private void OnError(object sender, ThreadExceptionEventArgs e)
        {
            DataAccess.InsertMessageAsync(e.Exception.Message, Strings.TransferringService,
                eMessageTypes.Error, e.Exception.StackTrace, false);
        }
    }
}
